﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace CannedHeater.Model
{
    /// <summary>
    /// Represents a complete data set that represents temperature and airflow data readings from the Netduino Device.
    /// Class is serializable - possible to save to some permanent storage for caching.
    /// </summary>

    [DataContract]
    public class DataHistory : INotifyPropertyChanged
    {
        private double _indoorTemp = Double.NaN;
        private string _indoorTempUnit = null;
        private double _outdoorTemp = Double.NaN;
        private string _outdoorTempUnit = null;
        private double _solarHeaterTemp = Double.NaN;
        private string _solarHeaterTempUnit = null;
        private double _airFlow = Double.NaN;
        private string _airFlowUnit = null;

        private SerializableKeyValuePair<DateTime, double>[] _indoorTempData;
        private SerializableKeyValuePair<DateTime, double>[] _outdoorTempData;
        private SerializableKeyValuePair<DateTime, double>[] _solarHeaterTempData;
        private SerializableKeyValuePair<DateTime, double>[] _airFlowData;

        [DataMember]
        public double IndoorTemp
        {
            get { return _indoorTemp; }
            set {
                _indoorTemp = value;
                RaisePropertyChanged("IndoorTemp"); 
            }
        }

        [DataMember]
        public double OutdoorTemp
        {
            get { return _outdoorTemp;  }
            set
            {
                _outdoorTemp = value;
                RaisePropertyChanged("OutdoorTemp");
            }
        }

        [DataMember]
        public double SolarHeaterTemp
        {
            get { return _solarHeaterTemp;  }
            set
            {
                _solarHeaterTemp = value;
                RaisePropertyChanged("SolarHeaterTemp");
            }
        }

        [DataMember]
        public double AirFlow
        {
            get { return _airFlow; }
            set
            {
                _airFlow = value;
                RaisePropertyChanged("AirFlow");
            }
        }

        [DataMember]
        public string IndoorTempUnit
        {
            get { return _indoorTempUnit; }
            set
            {
                _indoorTempUnit = value;
                RaisePropertyChanged("IndoorTempUnit");
            }
        }

        [DataMember]
        public string OutdoorTempUnit
        {
            get { return _outdoorTempUnit; }
            set
            {
                _outdoorTempUnit = value;
                RaisePropertyChanged("OutdoorTempUnit");
            }
        }

        [DataMember]
        public string SolarHeaterTempUnit
        {
            get { return _solarHeaterTempUnit; }
            set
            {
                _solarHeaterTempUnit = value;
                RaisePropertyChanged("SolarHeaterTempUnit");
            }
        }

        [DataMember]
        public string AirFlowUnit
        {
            get { return _airFlowUnit; }
            set
            {
                _airFlowUnit = value;
                RaisePropertyChanged("AirFlowUnit");
            }
        }

        [DataMember]
        public SerializableKeyValuePair<DateTime, double>[] IndoorTempData
        {
            get { return _indoorTempData; }
            set
            {
                _indoorTempData = value;
                RaisePropertyChanged("IndoorTempData");
            }
        }

        [DataMember]
        public SerializableKeyValuePair<DateTime, double>[] OutdoorTempData
        {
            get { return _outdoorTempData; }
            set
            {
                _outdoorTempData = value;
                RaisePropertyChanged("OutdoorTempData");
            }
        }

        [DataMember]
        public SerializableKeyValuePair<DateTime, double>[] SolarHeaterTempData
        {
            get { return _solarHeaterTempData; }
            set
            {
                _solarHeaterTempData = value;
                RaisePropertyChanged("SolarHeaterTempData");
            }
        }

        [DataMember]
        public SerializableKeyValuePair<DateTime, double>[] AirFlowData
        {
            get { return _airFlowData; }
            set
            {
                _airFlowData = value;
                RaisePropertyChanged("AirFlowData");
            }
        }


        /// <summary>
        /// Returns new DataHistory initialized with the data specified
        /// </summary>
        /// <param name="data">A dictionary where data block tags are associated with their PachubeData block</param>
        /// <param name="indoor">The data block tag for the indoor temperatures</param>
        /// <param name="outdoor">The data block tag for the outdoor temperatures</param>
        /// <param name="solarheater">The data block tag for the solar heater temperatures</param>
        /// <param name="airflow">The data block tag for the air flow</param>
        /// <returns>DataHistory object initialized with the data from the <paramref name="data"/> parameter</returns>
        public static DataHistory LoadHistoryFromPachubeData(Dictionary<string, Utilities.Pachube.PachubeData> data, string indoor, string outdoor, string solarheater, string airflow)
        {
            DataHistory dh = new DataHistory();
            if ( data.ContainsKey(indoor) )
            {
                Utilities.Pachube.PachubeData innerData = data[indoor];
                dh._indoorTemp = innerData.CurrentValue.Value;
                dh._indoorTempUnit = innerData.Unit;
                dh._indoorTempData = innerData.TimeValuePairs;
            }
            if (data.ContainsKey(outdoor))
            {
                Utilities.Pachube.PachubeData innerData = data[outdoor];
                dh._outdoorTemp = innerData.CurrentValue.Value;
                dh._outdoorTempUnit = innerData.Unit;
                dh._outdoorTempData = innerData.TimeValuePairs;
            }
            if (data.ContainsKey(solarheater))
            {
                Utilities.Pachube.PachubeData innerData = data[solarheater];
                dh._solarHeaterTemp = innerData.CurrentValue.Value;
                dh._solarHeaterTempUnit = innerData.Unit;
                dh._solarHeaterTempData = innerData.TimeValuePairs;
            }
            if (data.ContainsKey(airflow))
            {
                Utilities.Pachube.PachubeData innerData = data[airflow];
                dh._airFlow = innerData.CurrentValue.Value;
                dh._airFlowUnit = innerData.Unit;
                dh._airFlowData = innerData.TimeValuePairs;
            }
            return dh;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}
